<?php

namespace app\api\controller\v1;

use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\Litestorecart;
use app\admin\model\Litestorespecvalue;
use app\common\controller\Api;
use app\common\library\Auth;
use addons\litestore\model\CacheCart;

class Cart extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /* 加入购物车*/
    public function add()
    {

        $goods_id = $this->request->param('goods_id');
        $goods_num = $this->request->param('num');
        $goods_sku_id = $this->request->param('spec_sku_id');
        $user = $this->auth->getUser();
        $uid = $user['id'];

        $litestoreCartModel = new Litestorecart();
        $cart = $litestoreCartModel->where('goods_id', $goods_id)->where('uid', $uid)->where('spec_sku_id', $goods_sku_id)->find();
        if ($cart) {
            $goods_num = $goods_num + $cart['num'];
            $litestoreCartModel->where('id', $cart['id'])->update(['num' => $goods_num]);
        } else {
            $litestoreCartModel->insert(['uid' => $uid, 'goods_id' => $goods_id, 'num' => $goods_num, 'spec_sku_id' => $goods_sku_id]);
        }
        $total_num = $litestoreCartModel->getTotalNum($uid);
        return $this->success('加入购物车成功', ['cart_total_num' => $total_num]);
    }

    //获得购物车商品数量
    public function getTotalNum()
    {
        $litestoreCartModel = new Litestorecart();
        $user = $this->auth->getUser();
        $uid = $user['id'];
        $total_num = $litestoreCartModel->getTotalNum($uid);
        return $this->success('', ['cart_total_num' => $total_num]);
    }


    public function cartList()
    {

        $user = $this->auth->getUser();
        $uid = $user['id'];
        $litestoreCartModel = new Litestorecart();
        $query = $litestoreCartModel->with(['goods', 'spec'])->where('uid', $uid)->select();
        $list = array();


        $litestoreSpecValue = new Litestorespecvalue();
        foreach ($query as $key => $val) {
            $obj = array();
            $obj['uid'] = $val['uid'];
            $obj['num'] = $val['num'];
            $obj['spec_sku_id'] = $val['spec_sku_id'];
            $obj['goods_id'] = $val['goods_id'];
            $obj['goods_name'] = $val['goods']['goods_name'];
            $obj['goods_price'] = $val['spec']['goods_price'];

            $price = $val['spec']['goods_price'];
            if($user['group_id'] == 1){
                $obj['rank_price'] = $price;
            }else if($user['group_id'] == 2){
                $obj['rank_price'] = sprintf("%.2f",$price*$val['goods']['oneRatio']/100) ;
            }else if($user['group_id'] == 3){
                $obj['rank_price'] =   sprintf("%.2f",$price*$val['goods']['twoRatio']/100 );
            }else if($user['group_id']== 4){
                $obj['rank_price'] = sprintf("%.2f",$price*$val['goods']['threeRatio']/100 );;
            }
            if ($val['spec']['spec_image']) {
                $obj['image'] = cdnurl($val['spec']['spec_image'], true);
            } else {
                $obj['image'] = cdnurl(explode(",", $val['goods']['images'])[0], true);
            }


            $tagId = explode("_", $val['spec_sku_id']);
            $specValue = $litestoreSpecValue->where('id', 'in', $tagId)->select();
            $tag = "";
            foreach ($specValue as $k => $v) {
                $tag .= $v['spec_value'] . " ";
            }
            $obj['tag'] = $tag;
            $list[] = $obj;
        }
        $data['list'] = $list;
        $total_num = $litestoreCartModel->getTotalNum($uid);
        $data['cart_total_num'] = $total_num;
        $this->success('', $data);

    }

    public function sub()
    {
        $goods_id = $this->request->param('goods_id');
        $goods_num = $this->request->param('num');
        $goods_sku_id = $this->request->param('spec_sku_id');
        $litestoreCartModel = new Litestorecart();
        $user = $this->auth->getUser();
        $uid = $user['id'];
        $isUpdate = -1;
        if ($goods_num - 1 == 0) {
            $isUpdate = 1;
            $litestoreCartModel->where('goods_id', $goods_id)->where('uid', $uid)->where('spec_sku_id', $goods_sku_id)->delete();
        } else {
            $litestoreCartModel->where('goods_id', $goods_id)->where('uid', $uid)->where('spec_sku_id', $goods_sku_id)->setDec('num');
        }
        $total_num = $litestoreCartModel->getTotalNum($uid);
        $data = ['cart_total_num' => $total_num, 'isUpdate' => $isUpdate];
        return $this->success('', $data);
    }

    public function delete()
    {
        $rq_data = $this->request->request();
        $goods_id = $rq_data['goods_id'];
        $goods_sku_id = $rq_data['goods_sku_id'];
        $this->model->delete($goods_id, $goods_sku_id);
        return $this->success();
    }

}

